<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Laporan extends CI_Controller {
    var $outlet;

	public function __construct()
	{
		parent::__construct();
                $this->CI=& get_instance();
                $this->CI->load->config('config');
		$this->outlet = $this->CI->config->item('outlet');
                
		$this->load->model('akun_model');
                $this->load->model('transaksi_akun_model');
                $this->load->model('transaksi_keuangan_model');
				$this->load->model('batch_model');
	}

        // fungsi untuk menampilkan hasil laporan buku besar
	public function buku_besar()
	{
            $this->load->library('form_validation');
             $config = array(
                    array('field'=>'thn_mulai', 'label'=>'Tanggal Mulai', 'rules'=>'required'),
                    array('field'=>'thn_selesai','label'=>'Tanggal Akhir', 'rules'=>'required')
                );
			//dropdown batch
				unset($options);
				$batch = $this->batch_model->get_batch($this->session->userdata('id_lisensi'));
                $options[0] = '-- Pilih Batch --';
                foreach ($batch->result() as $row){
                    $options[$row->ID_BATCH] = $row->NAMA_BATCH;
                }
                $data['batch'] = $options;
				
            $this->form_validation->set_rules($config);
            $this->form_validation->set_message("required","%s tidak boleh kosong");
            $this->form_validation->set_error_delimiters('<p class="error">', '</p>');

            $data['title'] = 'Laporan Buku Besar';
            if ($this->form_validation->run() == FALSE){

		$this->template->write_view('header', 'template/header',$data);
		$this->template->write_view('content', 'keuangan/laporan_buku_besar_view',$data);
		$this->template->write_view('footer', 'template/footer',$data);
		$this->template->render();
            }else{
		$tgl_awal = $this->input->post('thn_mulai')."-".$this->input->post('bln_mulai')."-".$this->input->post('tgl_mulai');
                $tgl_akhir = $this->input->post('thn_selesai')."-".$this->input->post('bln_selesai')."-".$this->input->post('tgl_selesai');
                $id_batch = $this->input->post('id_batch');
                $data['tanggal_awal'] = $tgl_awal;
                $data['tanggal_akhir'] = $tgl_akhir;
                
                $laporan = $this->transaksi_akun_model->get_laporan_buku_besar($tgl_awal, $tgl_akhir, $id_batch);

                $buku_besar = array(); $no = 0;  $kode = '';
                foreach ($laporan->result() as $row){
                    if ($kode != $row->KODE_AKUN){
                        $kode = $row->KODE_AKUN;
                        $i=0;
                        $buku_besar[$row->KODE_AKUN] = array('kode'=>$row->KODE_AKUN, 'nama'=>$row->NAMA_AKUN);
                        $no++;
                    }
                    
                    $buku_besar[$row->KODE_AKUN]['detail'][$i] = array('tanggal'=>date('d M Y', strtotime($row->TANGGAL_TRANSAKSI)),
                            'no_bukti'=>$row->NO_BUKTI, 'keterangan'=>$row->KETERANGAN,
                        'debet'=>$row->STATUS_NOTE == 0 ? $row->JUMLAH_TRANSAKSI:0,
                        'kredit'=>$row->STATUS_NOTE == 1 ? $row->JUMLAH_TRANSAKSI:0);

                    $i++;
                }

                $data['laporan'] = $buku_besar;

		$this->load->view('keuangan/laporan_buku_besar',$data);
            }
	}

        // fungsi untuk menampilkan hasil laporan laba rugi
        function laba_rugi(){
            $this->load->library('form_validation');
             $config = array(
                    array('field'=>'thn_mulai', 'label'=>'Tanggal Mulai', 'rules'=>'required'),
                    array('field'=>'thn_selesai','label'=>'Tanggal Akhir', 'rules'=>'required')
                );
				
			//dropdown batch
				unset($options);
				$batch = $this->batch_model->get_batch($this->session->userdata('id_lisensi'));
                $options[0] = '-- Pilih Batch --';
                foreach ($batch->result() as $row){
                    $options[$row->ID_BATCH] = $row->NAMA_BATCH;
                }
                $data['batch'] = $options;
				
            $this->form_validation->set_rules($config);
            $this->form_validation->set_message("required","%s tidak boleh kosong");
            $this->form_validation->set_error_delimiters('<p class="error">', '</p>');

            $data['title'] = 'Laporan Laba/Rugi';
            if ($this->form_validation->run() == FALSE){

		$this->template->write_view('header', 'template/header',$data);
		$this->template->write_view('content', 'keuangan/laporan_laba_rugi_view',$data);
		$this->template->write_view('footer', 'template/footer',$data);
		$this->template->render();
            }else{                
                $tgl_awal = $this->input->post('thn_mulai')."-".$this->input->post('bln_mulai')."-".$this->input->post('tgl_mulai');
                $tgl_akhir = $this->input->post('thn_selesai')."-".$this->input->post('bln_selesai')."-".$this->input->post('tgl_selesai');
				$id_batch = $this->input->post('id_batch');
                $data['tanggal_awal'] = $tgl_awal;
                $data['tanggal_akhir'] = $tgl_akhir;

                $pendapatan = $this->akun_model->get_akun_byNama('Pendapatan');
                $beban = $this->akun_model->get_akun_byNama('Beban');

                $data['pendapatan'] = $this->transaksi_akun_model->get_transaksi_byAkun($pendapatan->row()->ID_AKUN);
                $data['beban'] = $this->transaksi_akun_model->get_transaksi_byAkun($beban->row()->ID_AKUN);

                $data['child_pendapatan'] = $this->transaksi_akun_model->get_transaksi_byAkunParent($pendapatan->row()->ID_AKUN);
                $data['child_beban'] = $this->transaksi_akun_model->get_transaksi_byAkunParent($beban->row()->ID_AKUN);

                $this->load->view('keuangan/laporan_laba_rugi',$data);
            }
        }

        function neraca(){
            $this->load->library('form_validation');
             $config = array(
                    array('field'=>'thn_selesai','label'=>'Tanggal', 'rules'=>'required')
                );
			
			//dropdown batch
				unset($options);
				$batch = $this->batch_model->get_batch($this->session->userdata('id_lisensi'));
                $options[0] = '-- Pilih Batch --';
                foreach ($batch->result() as $row){
                    $options[$row->ID_BATCH] = $row->NAMA_BATCH;
                }
                $data['batch'] = $options;
				
            $this->form_validation->set_rules($config);
            $this->form_validation->set_message("required","%s tidak boleh kosong");
            $this->form_validation->set_error_delimiters('<p class="error">', '</p>');

            $data['title'] = 'Laporan Neraca';
            if ($this->form_validation->run() == FALSE){

		$this->template->write_view('header', 'template/header',$data);
		$this->template->write_view('content', 'keuangan/laporan_neraca_view',$data);
		$this->template->write_view('footer', 'template/footer',$data);
		$this->template->render();
            }else{
                $tgl_awal = "0000-00-00";
                $tgl_akhir = $this->input->post('thn_selesai')."-".$this->input->post('bln_selesai')."-".$this->input->post('tgl_selesai');

                $data['tanggal_awal'] = $tgl_awal;
                $data['tanggal_akhir'] = $tgl_akhir;
                
                $aktiva = $this->akun_model->get_akun_byNama('Aktiva');
                $pasiva = $this->akun_model->get_akun_byNama('Pasiva');

                $data['aktiva'] = $this->transaksi_akun_model->get_transaksi_byAkun($aktiva->row()->ID_AKUN);
                $data['pasiva'] = $this->transaksi_akun_model->get_transaksi_byAkun($pasiva->row()->ID_AKUN);

                $data['child_aktiva'] = $this->transaksi_akun_model->get_transaksi_byAkunParent($aktiva->row()->ID_AKUN);
                $data['child_pasiva'] = $this->transaksi_akun_model->get_transaksi_byAkunParent($pasiva->row()->ID_AKUN);
				$id_batch = $this->input->post('id_batch');
				
                // hitung laba/rugi
                $data_pendapatan = $this->akun_model->get_akun_byNama('Pendapatan');
                $data_beban = $this->akun_model->get_akun_byNama('Beban');

                $pendapatan = $this->transaksi_akun_model->get_transaksi_byAkun($data_pendapatan->row()->ID_AKUN);
                $beban = $this->transaksi_akun_model->get_transaksi_byAkun($data_beban->row()->ID_AKUN);
                $child_pendapatan = $this->transaksi_akun_model->get_transaksi_byAkunParent($data_pendapatan->row()->ID_AKUN);
                $child_beban = $this->transaksi_akun_model->get_transaksi_byAkunParent($data_beban->row()->ID_AKUN);

                $jml_pendapatan = 0;
                foreach ($pendapatan->result() as $revenue){
                    foreach ($child_pendapatan->result() as $cpendapatan){
                        $total = $this->transaksi_akun_model->get_total_transaksi_byAkun($cpendapatan->ID_AKUN, 1, $tgl_awal, $tgl_akhir);
                        $jml_pendapatan += $total->num_rows() > 0 ? $total->row()->TOTAL_TRANSAKSI:0;
                        
                        $next_child = $this->transaksi_akun_model->get_transaksi_byAkunParent($cpendapatan->ID_AKUN);

                        foreach ($next_child->result() as $row){
                            $total = $this->transaksi_akun_model->get_total_transaksi_byAkun($row->ID_AKUN, 1, $tgl_awal, $tgl_akhir);
                            $jml_pendapatan += $total->num_rows() > 0 ? $total->row()->TOTAL_TRANSAKSI:0;
                        }
                    }
                }

                $jml_beban = 0;
                foreach ($beban->result() as $beban){
                    $jml_beban = $beban->TOTAL_TRANSAKSI;
                    foreach ($child_beban->result() as $cbeban){
                        $total = $this->transaksi_akun_model->get_total_transaksi_byAkun($cbeban->ID_AKUN, 0, $tgl_awal, $tgl_akhir);
                        $jml_beban += $total->num_rows() > 0 ? $total->row()->TOTAL_TRANSAKSI:0;

                        $next_child = $this->transaksi_akun_model->get_transaksi_byAkunParent($cbeban->ID_AKUN);
                        foreach ($next_child->result() as $row){
                            $total = $this->transaksi_akun_model->get_total_transaksi_byAkun($row->ID_AKUN, 0, $tgl_awal, $tgl_akhir);
                            $jml_beban += $total->num_rows() > 0 ? $total->row()->TOTAL_TRANSAKSI:0;
                        }
                    }
                }
                $data['labarugi'] = $jml_pendapatan - $jml_beban;
                
                $this->load->view('keuangan/laporan_neraca',$data);
            }
        }

        function deret(){
            $x = $this->fib2(7);
            echo number_format($x, 0);
            echo '<br>'.date('Y-m-10').'10';
        }

        function fib2($num){
            $x[0] = 0;
            $x[1] = 1; $sum = 0;

            for ($i=2; $i<=$num; $i++){
                $x[$i] = $x[$i-1]+$x[$i-2];
                $sum = $x[$i];
            }

            return $sum;
        }
}

/* End of file kasbon.php */
/* Location: ./application/controllers/keuangan/kasbon.php */